from django.contrib import auth
from django.shortcuts import render, redirect, HttpResponse
from django.views import View
from django.views.generic import DetailView
from blog.forms import *
from blog.models import Loguser

# Create your views here.


class IndexViews(View):
    def get(self, request):
        blog_list = Blog.objects.all()
        # print(request)
        return render(request, 'index.html', {'blog_list': blog_list})


class Registe(View):

    def post(self, request):
        form_obj = RegisteForm(request.POST, request.FILES)
        if form_obj.is_valid():
            form_obj.cleaned_data.pop('repassword')
            print(form_obj.cleaned_data)
            user_obj = Loguser.objects.create_user(**form_obj.cleaned_data, is_staff=1, is_superuser=1)
            auth.login(request, user_obj)
            return redirect('/login/')
        else:
            return render(request, 'blog/registe.html', {'form_obj': form_obj})

    def get(self, request):
        form_obj = RegisteForm()
        return render(request, 'blog/registe.html', {'form_obj': form_obj})


class Login(View):

    def post(self, request):
        form_obj = LoginForm(request.POST)
        if form_obj.is_valid():
            username = form_obj.cleaned_data.get('username')
            password = form_obj.cleaned_data.get('password')

            user = auth.authenticate(username=username, password=password)
            if user:
                auth.login(request, user)
                return redirect('/')
            else:
                return render(request, 'blog/login.html', {'form_obj': form_obj, 'error': '用户名或密码错误'})
        else:
            return render(request, 'blog/login.html', {'form_obj': form_obj, 'error': '用户名或密码错误'})

    def get(self, request):
        form_obj = LoginForm()
        return render(request, 'blog/login.html', {'form_obj': form_obj})


class BlogDetailView(View):
    def get(self, request, pk):
        blog_obj = Blog.objects.all().first()
        # blog_obj.increase_views()
        return render(request, 'blog/detail.html', {'blog': blog_obj})


class CategoryView(View):
    def get(self, request, pk):
        blog_list = Category.objects.filter(id=pk).Blog_set.all().order_by('-created_time')
        return render(request, 'category/detail.html', {'blog_list': blog_list})


class TagView(View):
    def get(self, request, pk):
        tag_list = Tag.objects.filter(id=pk).Blog_set.all().order_by('-created_time')
        return render(request, 'blog/tag_detail.html', {'tag_list': tag_list})


class MyIndexView(View):
    def get(self, request, userid):
        return HttpResponse('1')


def haystack_search(request):
    return HttpResponse('1')


class LogoutView(View):
    def get(self, request):
        auth.logout(request)
        return redirect('/')


def tag(request):
    return HttpResponse('1')


def archives(request):
    return HttpResponse('1')


class AuthorIndexView(View):
    def get(self, request, userid):
        return HttpResponse('1')